import geopandas as gpd
# Read in the data
full_data = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/DEC_lands")
# View the first five rows of the data
full_data.head()
| OBJECTID | CATEGORY | UNIT | FACILITY | CLASS | UMP | DESCRIPTIO | REGION | COUNTY | URL | SOURCE | UPDATE_ | OFFICE | ACRES | LANDS_UID | GREENCERT | SHAPE_AREA | SHAPE_LEN | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | FOR PRES DET PAR | CFP | HANCOCK FP DETACHED PARCEL | WILD FOREST | None | DELAWARE COUNTY DETACHED PARCEL | 4 | DELAWARE | http://www.dec.ny.gov/ | DELAWARE RPP | 5/12 | STAMFORD | 738.620192 | 103 | N | 2.990365e+06 | 7927.662385 | POLYGON ((486093.245 4635308.586, 486787.235 4... |
| 1 | 2 | FOR PRES DET PAR | CFP | HANCOCK FP DETACHED PARCEL | WILD FOREST | None | DELAWARE COUNTY DETACHED PARCEL | 4 | DELAWARE | http://www.dec.ny.gov/ | DELAWARE RPP | 5/12 | STAMFORD | 282.553140 | 1218 | N | 1.143940e+06 | 4776.375600 | POLYGON ((491931.514 4637416.256, 491305.424 4... |
| 2 | 3 | FOR PRES DET PAR | CFP | HANCOCK FP DETACHED PARCEL | WILD FOREST | None | DELAWARE COUNTY DETACHED PARCEL | 4 | DELAWARE | http://www.dec.ny.gov/ | DELAWARE RPP | 5/12 | STAMFORD | 234.291262 | 1780 | N | 9.485476e+05 | 5783.070364 | POLYGON ((486000.287 4635834.453, 485007.550 4... |
| 3 | 4 | FOR PRES DET PAR | CFP | GREENE COUNTY FP DETACHED PARCEL | WILD FOREST | None | None | 4 | GREENE | http://www.dec.ny.gov/ | GREENE RPP | 5/12 | STAMFORD | 450.106464 | 2060 | N | 1.822293e+06 | 7021.644833 | POLYGON ((541716.775 4675243.268, 541217.579 4... |
| 4 | 6 | FOREST PRESERVE | AFP | SARANAC LAKES WILD FOREST | WILD FOREST | SARANAC LAKES | None | 5 | ESSEX | http://www.dec.ny.gov/lands/22593.html | DECRP, ESSEX RPP | 12/96 | RAY BROOK | 69.702387 | 1517 | N | 2.821959e+05 | 2663.909932 | POLYGON ((583896.043 4909643.187, 583891.200 4... |
type(full_data)
geopandas.geodataframe.GeoDataFrame
data = full_data.loc[:, ["CLASS", "COUNTY", "geometry"]].copy()
# How many lands of each type are there?
data.CLASS.value_counts()
WILD FOREST 965 INTENSIVE USE 108 PRIMITIVE 60 WILDERNESS 52 ADMINISTRATIVE 17 UNCLASSIFIED 7 HISTORIC 5 PRIMITIVE BICYCLE CORRIDOR 4 CANOE AREA 1 Name: CLASS, dtype: int64
# Select lands that fall under the "WILD FOREST" or "WILDERNESS" category
wild_lands = data.loc[data.CLASS.isin(['WILD FOREST', 'WILDERNESS'])].copy()
wild_lands.head()
| CLASS | COUNTY | geometry | |
|---|---|---|---|
| 0 | WILD FOREST | DELAWARE | POLYGON ((486093.245 4635308.586, 486787.235 4... |
| 1 | WILD FOREST | DELAWARE | POLYGON ((491931.514 4637416.256, 491305.424 4... |
| 2 | WILD FOREST | DELAWARE | POLYGON ((486000.287 4635834.453, 485007.550 4... |
| 3 | WILD FOREST | GREENE | POLYGON ((541716.775 4675243.268, 541217.579 4... |
| 4 | WILD FOREST | ESSEX | POLYGON ((583896.043 4909643.187, 583891.200 4... |
wild_lands.plot()
<AxesSubplot:>
# View the first five entries in the "geometry" column
wild_lands.geometry.head()
0 POLYGON ((486093.245 4635308.586, 486787.235 4... 1 POLYGON ((491931.514 4637416.256, 491305.424 4... 2 POLYGON ((486000.287 4635834.453, 485007.550 4... 3 POLYGON ((541716.775 4675243.268, 541217.579 4... 4 POLYGON ((583896.043 4909643.187, 583891.200 4... Name: geometry, dtype: geometry
# Campsites in New York state (Point)
POI_data = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/DEC_pointsinterest/DEC_pointsinterest")
campsites = POI_data.loc[POI_data.ASSET=='PRIMITIVE CAMPSITE'].copy()
# Foot trails in New York state (LineString)
roads_trails = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/DEC_roadstrails/DEC_roadstrails")
trails = roads_trails.loc[roads_trails.ASSET=='FOOT TRAIL'].copy()
# County boundaries in New York state (Polygon)
counties = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/NY_county_boundaries/NY_county_boundaries")
# Define a base map with county boundaries
ax = counties.plot(figsize=(10,10), color='none', edgecolor='gainsboro', zorder=3)
# Add wild lands, campsites, and foot trails to the base map
wild_lands.plot(color='lightgreen', ax=ax)
campsites.plot(color='maroon', markersize=2, ax=ax)
trails.plot(color='black', markersize=1, ax=ax)
<AxesSubplot:>
#
import geopandas as gpd
import pandas as pd
# Load a GeoDataFrame containing regions in Ghana
regions = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/ghana/ghana/Regions")
print(regions.crs)
epsg:32630
# Create a DataFrame with health facilities in Ghana
facilities_df = pd.read_csv("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/ghana/ghana/health_facilities.csv")
# Convert the DataFrame to a GeoDataFrame
facilities = gpd.GeoDataFrame(facilities_df, geometry=gpd.points_from_xy(facilities_df.Longitude, facilities_df.Latitude))
# Set the coordinate reference system (CRS) to EPSG 4326
facilities.crs = {'init': 'epsg:4326'}
# View the first five rows of the GeoDataFrame
facilities.head()
/Users/db/opt/anaconda3/lib/python3.8/site-packages/pyproj/crs/crs.py:131: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 in_crs_string = _prepare_from_proj_string(in_crs_string)
| Region | District | FacilityName | Type | Town | Ownership | Latitude | Longitude | geometry | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Ashanti | Offinso North | A.M.E Zion Clinic | Clinic | Afrancho | CHAG | 7.40801 | -1.96317 | POINT (-1.96317 7.40801) |
| 1 | Ashanti | Bekwai Municipal | Abenkyiman Clinic | Clinic | Anwiankwanta | Private | 6.46312 | -1.58592 | POINT (-1.58592 6.46312) |
| 2 | Ashanti | Adansi North | Aboabo Health Centre | Health Centre | Aboabo No 2 | Government | 6.22393 | -1.34982 | POINT (-1.34982 6.22393) |
| 3 | Ashanti | Afigya-Kwabre | Aboabogya Health Centre | Health Centre | Aboabogya | Government | 6.84177 | -1.61098 | POINT (-1.61098 6.84177) |
| 4 | Ashanti | Kwabre | Aboaso Health Centre | Health Centre | Aboaso | Government | 6.84177 | -1.61098 | POINT (-1.61098 6.84177) |
# Create a map
ax = regions.plot(figsize=(8,8), color='whitesmoke', linestyle=':', edgecolor='black')
facilities.to_crs(epsg=32630).plot(markersize=1, ax=ax)
<AxesSubplot:>
# The "Latitude" and "Longitude" columns are unchanged
facilities.to_crs(epsg=32630).head()
| Region | District | FacilityName | Type | Town | Ownership | Latitude | Longitude | geometry | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | Ashanti | Offinso North | A.M.E Zion Clinic | Clinic | Afrancho | CHAG | 7.40801 | -1.96317 | POINT (614422.662 818986.851) |
| 1 | Ashanti | Bekwai Municipal | Abenkyiman Clinic | Clinic | Anwiankwanta | Private | 6.46312 | -1.58592 | POINT (656373.863 714616.547) |
| 2 | Ashanti | Adansi North | Aboabo Health Centre | Health Centre | Aboabo No 2 | Government | 6.22393 | -1.34982 | POINT (682573.395 688243.477) |
| 3 | Ashanti | Afigya-Kwabre | Aboabogya Health Centre | Health Centre | Aboabogya | Government | 6.84177 | -1.61098 | POINT (653484.490 756478.812) |
| 4 | Ashanti | Kwabre | Aboaso Health Centre | Health Centre | Aboaso | Government | 6.84177 | -1.61098 | POINT (653484.490 756478.812) |
# In case the EPSG code is not available in GeoPandas, we can change the CRS with what's known as the "proj4 string" of the CRS. For instance, the proj4 string to convert to latitude/longitude coordinates is as follows:
#+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs
# Change the CRS to EPSG 4326
regions.to_crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs").head()
| Region | geometry | |
|---|---|---|
| 0 | Ashanti | POLYGON ((-1.30985 7.62302, -1.30786 7.62198, ... |
| 1 | Brong Ahafo | POLYGON ((-2.54567 8.76089, -2.54473 8.76071, ... |
| 2 | Central | POLYGON ((-2.06723 6.29473, -2.06658 6.29420, ... |
| 3 | Eastern | POLYGON ((-0.21751 7.21009, -0.21747 7.20993, ... |
| 4 | Greater Accra | POLYGON ((0.23456 6.10986, 0.23484 6.10974, 0.... |
# Get the x-coordinate of each point
facilities.geometry.x.head()
0 -1.96317 1 -1.58592 2 -1.34982 3 -1.61098 4 -1.61098 dtype: float64
# Calculate the area (in square meters) of each polygon in the GeoDataFrame
regions.loc[:, "AREA"] = regions.geometry.area / 10**6
print("Area of Ghana: {} square kilometers".format(regions.AREA.sum()))
print("CRS:", regions.crs)
regions.head()
Area of Ghana: 239584.5760055668 square kilometers CRS: epsg:32630
| Region | geometry | AREA | |
|---|---|---|---|
| 0 | Ashanti | POLYGON ((686446.075 842986.894, 686666.193 84... | 24379.017777 |
| 1 | Brong Ahafo | POLYGON ((549970.457 968447.094, 550073.003 96... | 40098.168231 |
| 2 | Central | POLYGON ((603176.584 695877.238, 603248.424 69... | 9665.626760 |
| 3 | Eastern | POLYGON ((807307.254 797910.553, 807311.908 79... | 18987.625847 |
| 4 | Greater Accra | POLYGON ((858081.638 676424.913, 858113.115 67... | 3706.511145 |
#
import pandas as pd
import geopandas as gpd
import math
import folium
from folium import Choropleth, Circle, Marker
from folium.plugins import HeatMap, MarkerCluster
# Create a map
m_1 = folium.Map(location=[42.32,-71.0589], tiles='openstreetmap', zoom_start=10)
# Display the map
m_1
# Load the data
crimes = pd.read_csv("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/crimes-in-boston/crimes-in-boston/crime.csv", encoding='latin-1')
# Drop rows with missing locations
crimes.dropna(subset=['Lat', 'Long', 'DISTRICT'], inplace=True)
# Focus on major crimes in 2018
crimes = crimes[crimes.OFFENSE_CODE_GROUP.isin([
'Larceny', 'Auto Theft', 'Robbery', 'Larceny From Motor Vehicle', 'Residential Burglary',
'Simple Assault', 'Harassment', 'Ballistics', 'Aggravated Assault', 'Other Burglary',
'Arson', 'Commercial Burglary', 'HOME INVASION', 'Homicide', 'Criminal Harassment',
'Manslaughter'])]
crimes = crimes[crimes.YEAR>=2018]
# Print the first five rows of the table
crimes.head()
| INCIDENT_NUMBER | OFFENSE_CODE | OFFENSE_CODE_GROUP | OFFENSE_DESCRIPTION | DISTRICT | REPORTING_AREA | SHOOTING | OCCURRED_ON_DATE | YEAR | MONTH | DAY_OF_WEEK | HOUR | UCR_PART | STREET | Lat | Long | Location | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | I182070945 | 619 | Larceny | LARCENY ALL OTHERS | D14 | 808 | NaN | 2018-09-02 13:00:00 | 2018 | 9 | Sunday | 13 | Part One | LINCOLN ST | 42.357791 | -71.139371 | (42.35779134, -71.13937053) |
| 6 | I182070933 | 724 | Auto Theft | AUTO THEFT | B2 | 330 | NaN | 2018-09-03 21:25:00 | 2018 | 9 | Monday | 21 | Part One | NORMANDY ST | 42.306072 | -71.082733 | (42.30607218, -71.08273260) |
| 8 | I182070931 | 301 | Robbery | ROBBERY - STREET | C6 | 177 | NaN | 2018-09-03 20:48:00 | 2018 | 9 | Monday | 20 | Part One | MASSACHUSETTS AVE | 42.331521 | -71.070853 | (42.33152148, -71.07085307) |
| 19 | I182070915 | 614 | Larceny From Motor Vehicle | LARCENY THEFT FROM MV - NON-ACCESSORY | B2 | 181 | NaN | 2018-09-02 18:00:00 | 2018 | 9 | Sunday | 18 | Part One | SHIRLEY ST | 42.325695 | -71.068168 | (42.32569490, -71.06816778) |
| 24 | I182070908 | 522 | Residential Burglary | BURGLARY - RESIDENTIAL - NO FORCE | B2 | 911 | NaN | 2018-09-03 18:38:00 | 2018 | 9 | Monday | 18 | Part One | ANNUNCIATION RD | 42.335062 | -71.093168 | (42.33506218, -71.09316781) |
daytime_robberies = crimes[((crimes.OFFENSE_CODE_GROUP == 'Robbery') & \
(crimes.HOUR.isin(range(9,18))))]
# Create a map
m_2 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
# Add points to the map
for idx, row in daytime_robberies.iterrows():
Marker([row['Lat'], row['Long']]).add_to(m_2)
# Display the map
m_2
# Create the map
m_3 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
# Add points to the map
mc = MarkerCluster()
for idx, row in daytime_robberies.iterrows():
if not math.isnan(row['Long']) and not math.isnan(row['Lat']):
mc.add_child(Marker([row['Lat'], row['Long']]))
m_3.add_child(mc)
# Display the map
m_3
# Create a base map
m_4 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=13)
def color_producer(val):
if val <= 12:
return 'forestgreen'
else:
return 'darkred'
# Add a bubble map to the base map
for i in range(0,len(daytime_robberies)):
Circle(
location=[daytime_robberies.iloc[i]['Lat'], daytime_robberies.iloc[i]['Long']],
radius=20,
color=color_producer(daytime_robberies.iloc[i]['HOUR'])).add_to(m_4)
# Display the map
m_4
# Create a base map
m_5 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=12)
# Add a heatmap to the base map
HeatMap(data=crimes[['Lat', 'Long']], radius=10).add_to(m_5)
# Display the map
m_5
# GeoDataFrame with geographical boundaries of Boston police districts
districts_full = gpd.read_file('/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/Police_Districts/Police_Districts/Police_Districts.shp')
districts = districts_full[["DISTRICT", "geometry"]].set_index("DISTRICT")
districts.head()
| geometry | |
|---|---|
| DISTRICT | |
| A15 | MULTIPOLYGON (((-71.07416 42.39051, -71.07415 ... |
| A7 | MULTIPOLYGON (((-70.99644 42.39557, -70.99644 ... |
| A1 | POLYGON ((-71.05200 42.36884, -71.05169 42.368... |
| C6 | POLYGON ((-71.04406 42.35403, -71.04412 42.353... |
| D4 | POLYGON ((-71.07416 42.35724, -71.07359 42.357... |
# Number of crimes in each police district
plot_dict = crimes.DISTRICT.value_counts()
plot_dict.head()
D4 2885 B2 2231 A1 2130 C11 1899 B3 1421 Name: DISTRICT, dtype: int64
# Create a base map
m_6 = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=12)
# Add a choropleth map to the base map
Choropleth(geo_data=districts.__geo_interface__,
data=plot_dict,
key_on="feature.id",
fill_color='YlGnBu',
legend_name='Major criminal incidents (Jan-Aug 2018)'
).add_to(m_6)
# Display the map
m_6
#
!pip install geopy==1.22.0
Requirement already satisfied: geopy==1.22.0 in ./opt/anaconda3/lib/python3.8/site-packages (1.22.0) Requirement already satisfied: geographiclib<2,>=1.49 in ./opt/anaconda3/lib/python3.8/site-packages (from geopy==1.22.0) (1.52)
import pandas as pd
import geopandas as gpd
import numpy as np
import folium
from folium import Marker
from geopandas.tools import geocode
from geopy.exc import GeocoderTimedOut
def do_geocode(address, attempt=1, max_attempts=5):
try:
return geopy.geocode(address)
except GeocoderTimedOut:
if attempt <= max_attempts:
return do_geocode(address, attempt=attempt+1)
raise
result = geocode("University of Manchester, Manchester", provider="nominatim")
####(“University of Manchester”, “city=Manchester”, “type=University”)
result
| geometry | address | |
|---|---|---|
| 0 | POINT (-2.23361 53.47486) | Renold Building, University of Manchester, Alt... |
point = result.geometry.iloc[0]
print("Latitude:", point.y)
print("Longitude:", point.x)
Latitude: 53.474855250000005 Longitude: -2.2336140230927333
universities = pd.read_csv("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/top_universities.csv")
universities.head()
| Name | |
|---|---|
| 0 | University of Oxford |
| 1 | University of Cambridge |
| 2 | Imperial College London |
| 3 | ETH Zurich |
| 4 | UCL |
def my_geocoder(row):
try:
point = geocode(row, provider='nominatim').geometry.iloc[0]
return pd.Series({'Latitude': point.y, 'Longitude': point.x, 'geometry': point})
except:
return None
universities[['Latitude', 'Longitude', 'geometry']] = universities.apply(lambda x: my_geocoder(x['Name']), axis=1)
print("{}% of addresses were geocoded!".format(
(1 - sum(np.isnan(universities["Latitude"])) / len(universities)) * 100))
# Drop universities that were not successfully geocoded
universities = universities.loc[~np.isnan(universities["Latitude"])]
universities = gpd.GeoDataFrame(universities, geometry=universities.geometry)
universities.crs = {'init': 'epsg:4326'}
universities.head()
95.0% of addresses were geocoded!
/Users/db/opt/anaconda3/lib/python3.8/site-packages/pyproj/crs/crs.py:131: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6 in_crs_string = _prepare_from_proj_string(in_crs_string)
| Name | Latitude | Longitude | geometry | |
|---|---|---|---|---|
| 0 | University of Oxford | 51.750690 | -1.247482 | POINT (-1.24748 51.75069) |
| 1 | University of Cambridge | 52.199852 | 0.119739 | POINT (0.11974 52.19985) |
| 2 | Imperial College London | 51.498959 | -0.175641 | POINT (-0.17564 51.49896) |
| 3 | ETH Zurich | 47.408327 | 8.507564 | POINT (8.50756 47.40833) |
| 4 | UCL | 51.523581 | -0.132977 | POINT (-0.13298 51.52358) |
# Create a map
m = folium.Map(location=[54, 15], tiles='openstreetmap', zoom_start=2)
# Add points to the map
for idx, row in universities.iterrows():
Marker([row['Latitude'], row['Longitude']], popup=row['Name']).add_to(m)
# Display the map
m
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = world.loc[world.continent == 'Europe'].reset_index(drop=True)
europe_stats = europe[["name", "pop_est", "gdp_md_est"]]
europe_boundaries = europe[["name", "geometry"]]
europe_boundaries.head()
| name | geometry | |
|---|---|---|
| 0 | Russia | MULTIPOLYGON (((178.725 71.099, 180.000 71.516... |
| 1 | Norway | MULTIPOLYGON (((15.143 79.674, 15.523 80.016, ... |
| 2 | France | MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ... |
| 3 | Sweden | POLYGON ((11.027 58.856, 11.468 59.432, 12.300... |
| 4 | Belarus | POLYGON ((28.177 56.169, 29.230 55.918, 29.372... |
europe_stats.head()
| name | pop_est | gdp_md_est | |
|---|---|---|---|
| 0 | Russia | 142257519 | 3745000.0 |
| 1 | Norway | 5320045 | 364700.0 |
| 2 | France | 67106161 | 2699000.0 |
| 3 | Sweden | 9960487 | 498100.0 |
| 4 | Belarus | 9549747 | 165400.0 |
# Use an attribute join to merge data about countries in Europe
europe = europe_boundaries.merge(europe_stats, on="name")
europe.head()
| name | geometry | pop_est | gdp_md_est | |
|---|---|---|---|---|
| 0 | Russia | MULTIPOLYGON (((178.725 71.099, 180.000 71.516... | 142257519 | 3745000.0 |
| 1 | Norway | MULTIPOLYGON (((15.143 79.674, 15.523 80.016, ... | 5320045 | 364700.0 |
| 2 | France | MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ... | 67106161 | 2699000.0 |
| 3 | Sweden | POLYGON ((11.027 58.856, 11.468 59.432, 12.300... | 9960487 | 498100.0 |
| 4 | Belarus | POLYGON ((28.177 56.169, 29.230 55.918, 29.372... | 9549747 | 165400.0 |
# Use spatial join to match universities to countries in Europe
european_universities = gpd.sjoin(universities, europe)
# Investigate the result
print("We located {} universities.".format(len(universities)))
print("Only {} of the universities were located in Europe (in {} different countries).".format(
len(european_universities), len(european_universities.name.unique())))
european_universities.head()
We located 95 universities. Only 90 of the universities were located in Europe (in 15 different countries).
<ipython-input-48-146764234e5d>:2: UserWarning: CRS mismatch between the CRS of left geometries and the CRS of right geometries. Use `to_crs()` to reproject one of the input geometries to match the CRS of the other. Left CRS: +init=epsg:4326 +type=crs Right CRS: EPSG:4326 european_universities = gpd.sjoin(universities, europe)
| Name | Latitude | Longitude | geometry | index_right | name | pop_est | gdp_md_est | |
|---|---|---|---|---|---|---|---|---|
| 0 | University of Oxford | 51.750690 | -1.247482 | POINT (-1.24748 51.75069) | 28 | United Kingdom | 64769452 | 2788000.0 |
| 1 | University of Cambridge | 52.199852 | 0.119739 | POINT (0.11974 52.19985) | 28 | United Kingdom | 64769452 | 2788000.0 |
| 2 | Imperial College London | 51.498959 | -0.175641 | POINT (-0.17564 51.49896) | 28 | United Kingdom | 64769452 | 2788000.0 |
| 4 | UCL | 51.523581 | -0.132977 | POINT (-0.13298 51.52358) | 28 | United Kingdom | 64769452 | 2788000.0 |
| 5 | London School of Economics and Political Science | 51.514311 | -0.116781 | POINT (-0.11678 51.51431) | 28 | United Kingdom | 64769452 | 2788000.0 |
#
import folium
from folium import Marker, GeoJson
from folium.plugins import HeatMap
import pandas as pd
import geopandas as gpd
releases = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/toxic_release_pennsylvania/toxic_release_pennsylvania/toxic_release_pennsylvania.shp")
releases.head()
| YEAR | CITY | COUNTY | ST | LATITUDE | LONGITUDE | CHEMICAL | UNIT_OF_ME | TOTAL_RELE | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2016 | PHILADELPHIA | PHILADELPHIA | PA | 40.005901 | -75.072103 | FORMIC ACID | Pounds | 0.160 | POINT (2718560.227 256380.179) |
| 1 | 2016 | PHILADELPHIA | PHILADELPHIA | PA | 39.920120 | -75.146410 | ETHYLENE GLYCOL | Pounds | 13353.480 | POINT (2698674.606 224522.905) |
| 2 | 2016 | PHILADELPHIA | PHILADELPHIA | PA | 40.023880 | -75.220450 | CERTAIN GLYCOL ETHERS | Pounds | 104.135 | POINT (2676833.394 261701.856) |
| 3 | 2016 | PHILADELPHIA | PHILADELPHIA | PA | 39.913540 | -75.198890 | LEAD COMPOUNDS | Pounds | 1730.280 | POINT (2684030.004 221697.388) |
| 4 | 2016 | PHILADELPHIA | PHILADELPHIA | PA | 39.913540 | -75.198890 | BENZENE | Pounds | 39863.290 | POINT (2684030.004 221697.388) |
stations = gpd.read_file("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/PhillyHealth_Air_Monitoring_Stations/PhillyHealth_Air_Monitoring_Stations/PhillyHealth_Air_Monitoring_Stations.shp")
stations.head()
| SITE_NAME | ADDRESS | BLACK_CARB | ULTRAFINE_ | CO | SO2 | OZONE | NO2 | NOY_NO | PM10 | ... | PAMS_VOC | TSP_11101 | TSP_METALS | TSP_LEAD | TOXICS_TO1 | MET | COMMUNITY_ | LATITUDE | LONGITUDE | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | LAB | 1501 East Lycoming Avenue | N | N | Y | N | Y | Y | Y | N | ... | Y | N | Y | N | y | N | N | 40.008606 | -75.097624 | POINT (2711384.641 257149.310) |
| 1 | ROX | Eva and Dearnley Streets | N | N | N | N | N | N | N | N | ... | N | N | Y | N | Y | N | N | 40.050461 | -75.236966 | POINT (2671934.290 271248.900) |
| 2 | NEA | Grant Avenue and Ashton Street | N | N | N | N | Y | N | N | N | ... | N | N | N | N | N | Y | N | 40.072073 | -75.013128 | POINT (2734326.638 280980.247) |
| 3 | CHS | 500 South Broad Street | N | N | N | N | N | N | N | N | ... | N | N | Y | N | Y | N | N | 39.944510 | -75.165442 | POINT (2693078.580 233247.101) |
| 4 | NEW | 2861 Lewis Street | N | N | Y | Y | Y | N | Y | Y | ... | N | Y | N | Y | N | Y | N | 39.991688 | -75.080378 | POINT (2716399.773 251134.976) |
5 rows × 24 columns
print(stations.crs)
print(releases.crs)
epsg:2272 epsg:2272
# Select one release incident in particular
recent_release = releases.iloc[360]
# Measure distance from release to each station
distances = stations.geometry.distance(recent_release.geometry)
distances
0 44778.509761 1 51006.456589 2 77744.509207 3 14672.170878 4 43753.554393 5 4711.658655 6 23197.430858 7 12072.823097 8 79081.825506 9 3780.623591 10 27577.474903 11 19818.381002 dtype: float64
print('Mean distance to monitoring stations: {} feet'.format(distances.mean()))
Mean distance to monitoring stations: 33516.28487007786 feet
print('Closest monitoring station ({} feet):'.format(distances.min()))
print(stations.iloc[distances.idxmin()][["ADDRESS", "LATITUDE", "LONGITUDE"]])
Closest monitoring station (3780.623590556444 feet): ADDRESS 3100 Penrose Ferry Road LATITUDE 39.91279 LONGITUDE -75.185448 Name: 9, dtype: object
two_mile_buffer = stations.geometry.buffer(2*5280)
two_mile_buffer.head()
0 POLYGON ((2721944.641 257149.310, 2721893.792 ... 1 POLYGON ((2682494.290 271248.900, 2682443.441 ... 2 POLYGON ((2744886.638 280980.247, 2744835.789 ... 3 POLYGON ((2703638.580 233247.101, 2703587.731 ... 4 POLYGON ((2726959.773 251134.976, 2726908.924 ... dtype: geometry
# Create map with release incidents and monitoring stations
m = folium.Map(location=[39.9526,-75.1652], zoom_start=11)
HeatMap(data=releases[['LATITUDE', 'LONGITUDE']], radius=15).add_to(m)
for idx, row in stations.iterrows():
Marker([row['LATITUDE'], row['LONGITUDE']]).add_to(m)
# Plot each polygon on the map
GeoJson(two_mile_buffer.to_crs(epsg=4326)).add_to(m)
# Show the map
m
# Turn group of polygons into single multipolygon
my_union = two_mile_buffer.geometry.unary_union
print('Type:', type(my_union))
# Show the MultiPolygon object
my_union
Type: <class 'shapely.geometry.multipolygon.MultiPolygon'>
# The closest station is less than two miles away
my_union.contains(releases.iloc[360].geometry)
True
# The closest station is more than two miles away
my_union.contains(releases.iloc[358].geometry)
False
#
# Imports
import pandas as pd
from datetime import date, timedelta
import folium
from folium import Marker
from folium.plugins import MarkerCluster
import math
import matplotlib.pyplot as plt
import seaborn as sns
# Population Data
populationData = pd.read_csv('/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/archive (3)/2019_Census_US_Population_Data_By_State_Lat_Long.csv')
#/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/Europe
# Get the most recent date for filtering
freshDate = date.today() - timedelta(days=1)
freshDate = date.strftime(freshDate,"%Y%m%d")
freshDate = freshDate[0:4] + "-" + freshDate[4:6] + "-" + freshDate[6:8]
# Vaccination data, for most recent date
vaccinationData = pd.read_csv('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/vaccinations/us_state_vaccinations.csv')
vaccinationByLocation = vaccinationData.loc[(vaccinationData.date == freshDate)][["location", "people_vaccinated"]]
# Vaccination and population data
vaccinationAndPopulationByLocation = pd.merge(populationData, vaccinationByLocation, left_on='STATE',right_on='location').drop(columns="location")
# Calculate percentage vaccinated by state
vaccinationAndPopulationByLocation["percent_vaccinated"] = vaccinationAndPopulationByLocation["people_vaccinated"] / vaccinationAndPopulationByLocation["POPESTIMATE2019"]
vaccinationAndPopulationByLocation
| STATE | POPESTIMATE2019 | lat | long | people_vaccinated | percent_vaccinated | |
|---|---|---|---|---|---|---|
| 0 | Alabama | 4903185 | 32.377716 | -86.300568 | 3075480.0 | 0.627241 |
| 1 | Alaska | 731545 | 58.301598 | -134.420212 | 510725.0 | 0.698146 |
| 2 | Arizona | 7278717 | 33.448143 | -112.096962 | 5318110.0 | 0.730638 |
| 3 | Arkansas | 3017804 | 34.746613 | -92.288986 | 2012662.0 | 0.666929 |
| 4 | California | 39512223 | 38.576668 | -121.493629 | 33093109.0 | 0.837541 |
| 5 | Colorado | 5758736 | 39.739227 | -104.984856 | 4570138.0 | 0.793601 |
| 6 | Connecticut | 3565287 | 41.764046 | -72.682198 | 3408122.0 | 0.955918 |
| 7 | Delaware | 973764 | 39.157307 | -75.519722 | 809259.0 | 0.831063 |
| 8 | District of Columbia | 705749 | 38.895110 | -77.036370 | 713148.0 | 1.010484 |
| 9 | Florida | 21477737 | 30.438118 | -84.281296 | 17035838.0 | 0.793186 |
| 10 | Georgia | 10617423 | 33.749027 | -84.388229 | 6937891.0 | 0.653444 |
| 11 | Hawaii | 1415872 | 21.307442 | -157.857376 | 1237468.0 | 0.873997 |
| 12 | Idaho | 1787065 | 43.617775 | -116.199722 | 1092082.0 | 0.611104 |
| 13 | Illinois | 12671821 | 39.798363 | -89.654961 | 9719223.0 | 0.766995 |
| 14 | Indiana | 6732219 | 39.768623 | -86.162643 | 4136805.0 | 0.614479 |
| 15 | Iowa | 3155070 | 41.591087 | -93.603729 | 2143373.0 | 0.679342 |
| 16 | Kansas | 2913314 | 39.048191 | -95.677956 | 2169971.0 | 0.744846 |
| 17 | Kentucky | 4467673 | 38.186722 | -84.875374 | 2955530.0 | 0.661537 |
| 18 | Louisiana | 4648794 | 30.457069 | -91.187393 | 2835880.0 | 0.610025 |
| 19 | Maine | 1344212 | 44.307167 | -69.781693 | 1216113.0 | 0.904703 |
| 20 | Maryland | 6045680 | 38.978764 | -76.490936 | 5227858.0 | 0.864726 |
| 21 | Massachusetts | 6892503 | 42.358162 | -71.063698 | 6767555.0 | 0.981872 |
| 22 | Michigan | 9986857 | 42.733635 | -84.555328 | 6687389.0 | 0.669619 |
| 23 | Minnesota | 5639632 | 44.955097 | -93.102211 | 4233903.0 | 0.750741 |
| 24 | Mississippi | 2976149 | 32.303848 | -90.182106 | 1773298.0 | 0.595836 |
| 25 | Missouri | 6137428 | 38.579201 | -92.172935 | 4061666.0 | 0.661786 |
| 26 | Montana | 1068778 | 46.585709 | -112.018417 | 696858.0 | 0.652014 |
| 27 | Nebraska | 1934408 | 40.808075 | -96.699654 | 1359511.0 | 0.702805 |
| 28 | Nevada | 3080156 | 39.163914 | -119.766121 | 2317905.0 | 0.752528 |
| 29 | New Hampshire | 1359711 | 43.206898 | -71.537994 | 1197591.0 | 0.880769 |
| 30 | New Jersey | 8882190 | 40.220596 | -74.769913 | 8027199.0 | 0.903741 |
| 31 | New Mexico | 2096829 | 35.682240 | -105.939728 | 1838772.0 | 0.876930 |
| 32 | North Carolina | 10488084 | 35.780430 | -78.639099 | 8831236.0 | 0.842026 |
| 33 | North Dakota | 762062 | 46.820850 | -100.783318 | 494374.0 | 0.648732 |
| 34 | Ohio | 11689100 | 39.961346 | -82.999069 | 7428524.0 | 0.635509 |
| 35 | Oklahoma | 3956971 | 35.492207 | -97.503342 | 2812852.0 | 0.710860 |
| 36 | Oregon | 4217737 | 44.938461 | -123.030403 | 3285372.0 | 0.778942 |
| 37 | Pennsylvania | 12801989 | 40.264378 | -76.883598 | 10866570.0 | 0.848819 |
| 38 | Rhode Island | 1059361 | 41.830914 | -71.414963 | 1043484.0 | 0.985013 |
| 39 | South Carolina | 5148714 | 34.000343 | -81.033211 | 3465819.0 | 0.673143 |
| 40 | South Dakota | 884659 | 44.367031 | -100.346405 | 676812.0 | 0.765054 |
| 41 | Tennessee | 6829174 | 36.165810 | -86.784241 | 4245134.0 | 0.621617 |
| 42 | Texas | 28995881 | 30.274670 | -97.740349 | 21205061.0 | 0.731313 |
| 43 | Utah | 3205958 | 40.777477 | -111.888237 | 2311410.0 | 0.720973 |
| 44 | Vermont | 623989 | 44.262436 | -72.580536 | 583312.0 | 0.934811 |
| 45 | Virginia | 8535519 | 37.538857 | -77.433640 | 7314619.0 | 0.856962 |
| 46 | Washington | 7614893 | 47.035805 | -122.905014 | 6150927.0 | 0.807750 |
| 47 | West Virginia | 1792147 | 38.336246 | -81.612328 | 1164555.0 | 0.649810 |
| 48 | Wisconsin | 5822434 | 43.074684 | -89.384445 | 4182104.0 | 0.718274 |
| 49 | Wyoming | 578759 | 41.140259 | -104.820236 | 340391.0 | 0.588139 |
print("Date ran:", date.today())
# Calculate the total percent vaccinated in the US
percentageTotal = vaccinationAndPopulationByLocation["people_vaccinated"].sum() / vaccinationAndPopulationByLocation["POPESTIMATE2019"].sum()
print('Percentage Vaccinated in the US: {}%'.format(round(percentageTotal*100, 2)))
Date ran: 2022-05-03 Percentage Vaccinated in the US: 76.29%
# Create the map
v_map = folium.Map(location=[42.32,-71.0589], tiles='cartodbpositron', zoom_start=4)
# Add points to the map
mc = MarkerCluster()
for idx, row in vaccinationAndPopulationByLocation.iterrows():
if not math.isnan(row['long']) and not math.isnan(row['lat']):
mc.add_child(Marker(location=[row['lat'], row['long']],
tooltip=str(round(row['percent_vaccinated']*100, 2))+"%"))
v_map.add_child(mc)
# Display the map
v_map
#
#import pandas as pd
#import geopandas as gpd
#import gmaps
#import gmaps.datasets
#import matplotlib.pyplot as plt
#gmaps.configure(api_key='YOUR_GOOGLE_MAPS_API_KEY')
# unfinished - more code needed.
#
import pandas as pd
import geopandas as gpd
#import gmaps
#import gmaps.datasets
import matplotlib.pyplot as plt
#Directory where the shapefile is.
shapefile = "/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/Europe/Europe"
europa_gdf = gpd.read_file(shapefile + '.shp')
europa_gdf
| NAME | ORGN_NAME | geometry | |
|---|---|---|---|
| 0 | Albania | Shqipëria | MULTIPOLYGON (((19.50115 40.96230, 19.50563 40... |
| 1 | Andorra | Andorra | POLYGON ((1.43992 42.60649, 1.45041 42.60596, ... |
| 2 | Austria | Österreich | POLYGON ((16.00000 48.77775, 16.00000 48.78252... |
| 3 | Belgium | België / Belgique | POLYGON ((5.00000 49.79374, 4.99724 49.79696, ... |
| 4 | Bosnia Herzegovina | Bosna i Hercegovina | POLYGON ((19.22947 43.53458, 19.22925 43.53597... |
| 5 | Croatia | Hrvatska | MULTIPOLYGON (((14.30038 44.50156, 14.28972 44... |
| 6 | Czech Republic | Cesko | POLYGON ((14.82523 50.87399, 14.83687 50.86996... |
| 7 | Denmark | Danmark | MULTIPOLYGON (((11.99978 54.94118, 11.98534 54... |
| 8 | Estonia | Eesti | MULTIPOLYGON (((23.97511 58.09691, 23.96645 58... |
| 9 | Finland | Suomi | MULTIPOLYGON (((22.07310 60.22830, 22.06502 60... |
| 10 | France | France | MULTIPOLYGON (((-2.28137 46.68570, -2.31121 46... |
| 11 | Germany | Deutschland | MULTIPOLYGON (((13.11717 54.54924, 13.12529 54... |
| 12 | Gibraltar (UK) | Gibraltar (UK) | POLYGON ((-5.35322 36.15980, -5.34003 36.15975... |
| 13 | Greece | Elláda | MULTIPOLYGON (((24.74283 37.59560, 24.73534 37... |
| 14 | Guernsey (UK) | Guernsey (UK) | MULTIPOLYGON (((-2.52483 49.48867, -2.52034 49... |
| 15 | Hungary | Magyarország | POLYGON ((22.32725 48.36194, 22.32294 48.32583... |
| 16 | Ireland | Éire / Ireland | MULTIPOLYGON (((-10.21726 51.74543, -10.22042 ... |
| 17 | Isle of Man (UK) | Isle of Man (UK) | MULTIPOLYGON (((-4.38349 54.32594, -4.37470 54... |
| 18 | Italy | Italia | MULTIPOLYGON (((12.46117 37.88075, 12.45686 37... |
| 19 | Jersey (UK) | Jersey (UK) | POLYGON ((-2.03507 49.19905, -2.03699 49.19314... |
| 20 | Latvia | Latvija | POLYGON ((22.14997 57.64129, 22.17712 57.65100... |
| 21 | Liechtenstein | Liechtenstein | POLYGON ((9.53591 47.27354, 9.56037 47.24912, ... |
| 22 | Lithuania | Lietuva | MULTIPOLYGON (((26.22030 55.00000, 26.21693 54... |
| 23 | Luxembourg | Lëtzebuerg / Luxemburg / Luxembourg | POLYGON ((6.13802 50.13290, 6.14092 50.12927, ... |
| 24 | Macedonia | Makedonija | POLYGON ((22.37382 42.32667, 22.37228 42.32576... |
| 25 | Malta | Malta | MULTIPOLYGON (((14.33938 36.00751, 14.33490 36... |
| 26 | Monaco | Monaco | POLYGON ((7.40680 43.73196, 7.40175 43.73449, ... |
| 27 | Montenegro | Crna Gora | MULTIPOLYGON (((19.22947 43.53458, 19.22795 43... |
| 28 | Netherlands | Nederland | MULTIPOLYGON (((6.05039 53.44675, 6.04538 53.4... |
| 29 | Norway | Norge | MULTIPOLYGON (((14.82404 68.21677, 14.80662 68... |
| 30 | Poland | Polska | MULTIPOLYGON (((14.21288 53.86478, 14.21349 53... |
| 31 | Portugal | Portugal | MULTIPOLYGON (((-25.83763 37.88595, -25.83689 ... |
| 32 | San Marino | San Marino | POLYGON ((12.51004 43.99981, 12.51557 43.99566... |
| 33 | Serbia | Srbija | POLYGON ((18.83875 45.90742, 18.86463 45.91018... |
| 34 | Slovakia | Slovensko | POLYGON ((18.85061 49.52131, 18.85416 49.51941... |
| 35 | Slovenia | Slovenija | POLYGON ((16.56602 46.48372, 16.54178 46.48717... |
| 36 | Spain | España | MULTIPOLYGON (((-5.60904 36.00032, -5.61149 36... |
| 37 | Sweden | Sverige | MULTIPOLYGON (((16.69817 57.43244, 16.69456 57... |
| 38 | Switzerland | Schweiz / Suisse / Svizerra / Svizra | POLYGON ((9.53591 47.27354, 9.52660 47.25858, ... |
| 39 | United Kingdom | United Kingdom | MULTIPOLYGON (((-5.33523 51.85830, -5.34557 51... |
| 40 | Armenia | Hayastan | MULTIPOLYGON (((45.48185 40.65776, 45.49001 40... |
| 41 | Azerbaijan | Azerbaycan | MULTIPOLYGON (((49.02264 39.19647, 49.02892 39... |
| 42 | Belarus | Belarus | POLYGON ((30.00000 51.48946, 29.95314 51.48722... |
| 43 | Bulgaria | Balgarija | POLYGON ((22.68183 44.21765, 22.68898 44.21381... |
| 44 | Faeroe Islands (Denmark) | Foroyar (Danmark) | MULTIPOLYGON (((-6.37378 62.23575, -6.38222 62... |
| 45 | Georgia | Sakartvelo | POLYGON ((40.01024 43.38326, 40.01102 43.39860... |
| 46 | Iceland | Ísland | MULTIPOLYGON (((-15.38783 64.35684, -15.39691 ... |
| 47 | Jan Mayen (Norway) | Jan Mayen (Norge) | POLYGON ((-8.13727 71.14619, -8.09449 71.14722... |
| 48 | Moldova | Moldova | POLYGON ((27.72524 48.45466, 27.74713 48.45837... |
| 49 | Romania | România | MULTIPOLYGON (((29.59458 44.81385, 29.57739 44... |
| 50 | Svalbard (Norway) | Svalbard (Norge) | MULTIPOLYGON (((20.80218 78.56455, 20.80361 78... |
| 51 | Turkey | Türkiye | MULTIPOLYGON (((29.11932 40.83066, 29.11734 40... |
| 52 | Ukraine | Ukrajina | MULTIPOLYGON (((33.52988 45.87363, 33.52778 45... |
| 53 | Russia | Rossíya | MULTIPOLYGON (((52.65438 71.45161, 52.64216 71... |
europa_gdf = europa_gdf.drop("ORGN_NAME", axis = 1)
europa_gdf
| NAME | geometry | |
|---|---|---|
| 0 | Albania | MULTIPOLYGON (((19.50115 40.96230, 19.50563 40... |
| 1 | Andorra | POLYGON ((1.43992 42.60649, 1.45041 42.60596, ... |
| 2 | Austria | POLYGON ((16.00000 48.77775, 16.00000 48.78252... |
| 3 | Belgium | POLYGON ((5.00000 49.79374, 4.99724 49.79696, ... |
| 4 | Bosnia Herzegovina | POLYGON ((19.22947 43.53458, 19.22925 43.53597... |
| 5 | Croatia | MULTIPOLYGON (((14.30038 44.50156, 14.28972 44... |
| 6 | Czech Republic | POLYGON ((14.82523 50.87399, 14.83687 50.86996... |
| 7 | Denmark | MULTIPOLYGON (((11.99978 54.94118, 11.98534 54... |
| 8 | Estonia | MULTIPOLYGON (((23.97511 58.09691, 23.96645 58... |
| 9 | Finland | MULTIPOLYGON (((22.07310 60.22830, 22.06502 60... |
| 10 | France | MULTIPOLYGON (((-2.28137 46.68570, -2.31121 46... |
| 11 | Germany | MULTIPOLYGON (((13.11717 54.54924, 13.12529 54... |
| 12 | Gibraltar (UK) | POLYGON ((-5.35322 36.15980, -5.34003 36.15975... |
| 13 | Greece | MULTIPOLYGON (((24.74283 37.59560, 24.73534 37... |
| 14 | Guernsey (UK) | MULTIPOLYGON (((-2.52483 49.48867, -2.52034 49... |
| 15 | Hungary | POLYGON ((22.32725 48.36194, 22.32294 48.32583... |
| 16 | Ireland | MULTIPOLYGON (((-10.21726 51.74543, -10.22042 ... |
| 17 | Isle of Man (UK) | MULTIPOLYGON (((-4.38349 54.32594, -4.37470 54... |
| 18 | Italy | MULTIPOLYGON (((12.46117 37.88075, 12.45686 37... |
| 19 | Jersey (UK) | POLYGON ((-2.03507 49.19905, -2.03699 49.19314... |
| 20 | Latvia | POLYGON ((22.14997 57.64129, 22.17712 57.65100... |
| 21 | Liechtenstein | POLYGON ((9.53591 47.27354, 9.56037 47.24912, ... |
| 22 | Lithuania | MULTIPOLYGON (((26.22030 55.00000, 26.21693 54... |
| 23 | Luxembourg | POLYGON ((6.13802 50.13290, 6.14092 50.12927, ... |
| 24 | Macedonia | POLYGON ((22.37382 42.32667, 22.37228 42.32576... |
| 25 | Malta | MULTIPOLYGON (((14.33938 36.00751, 14.33490 36... |
| 26 | Monaco | POLYGON ((7.40680 43.73196, 7.40175 43.73449, ... |
| 27 | Montenegro | MULTIPOLYGON (((19.22947 43.53458, 19.22795 43... |
| 28 | Netherlands | MULTIPOLYGON (((6.05039 53.44675, 6.04538 53.4... |
| 29 | Norway | MULTIPOLYGON (((14.82404 68.21677, 14.80662 68... |
| 30 | Poland | MULTIPOLYGON (((14.21288 53.86478, 14.21349 53... |
| 31 | Portugal | MULTIPOLYGON (((-25.83763 37.88595, -25.83689 ... |
| 32 | San Marino | POLYGON ((12.51004 43.99981, 12.51557 43.99566... |
| 33 | Serbia | POLYGON ((18.83875 45.90742, 18.86463 45.91018... |
| 34 | Slovakia | POLYGON ((18.85061 49.52131, 18.85416 49.51941... |
| 35 | Slovenia | POLYGON ((16.56602 46.48372, 16.54178 46.48717... |
| 36 | Spain | MULTIPOLYGON (((-5.60904 36.00032, -5.61149 36... |
| 37 | Sweden | MULTIPOLYGON (((16.69817 57.43244, 16.69456 57... |
| 38 | Switzerland | POLYGON ((9.53591 47.27354, 9.52660 47.25858, ... |
| 39 | United Kingdom | MULTIPOLYGON (((-5.33523 51.85830, -5.34557 51... |
| 40 | Armenia | MULTIPOLYGON (((45.48185 40.65776, 45.49001 40... |
| 41 | Azerbaijan | MULTIPOLYGON (((49.02264 39.19647, 49.02892 39... |
| 42 | Belarus | POLYGON ((30.00000 51.48946, 29.95314 51.48722... |
| 43 | Bulgaria | POLYGON ((22.68183 44.21765, 22.68898 44.21381... |
| 44 | Faeroe Islands (Denmark) | MULTIPOLYGON (((-6.37378 62.23575, -6.38222 62... |
| 45 | Georgia | POLYGON ((40.01024 43.38326, 40.01102 43.39860... |
| 46 | Iceland | MULTIPOLYGON (((-15.38783 64.35684, -15.39691 ... |
| 47 | Jan Mayen (Norway) | POLYGON ((-8.13727 71.14619, -8.09449 71.14722... |
| 48 | Moldova | POLYGON ((27.72524 48.45466, 27.74713 48.45837... |
| 49 | Romania | MULTIPOLYGON (((29.59458 44.81385, 29.57739 44... |
| 50 | Svalbard (Norway) | MULTIPOLYGON (((20.80218 78.56455, 20.80361 78... |
| 51 | Turkey | MULTIPOLYGON (((29.11932 40.83066, 29.11734 40... |
| 52 | Ukraine | MULTIPOLYGON (((33.52988 45.87363, 33.52778 45... |
| 53 | Russia | MULTIPOLYGON (((52.65438 71.45161, 52.64216 71... |
europa_gdf.plot()
<AxesSubplot:>
percapita_df = pd.read_csv("/Users/db/Documents/Kaggle python tutorials/Geopandas/maps/Europe/percapita.csv")
europa_gdf["percapita"] = percapita_df["percapita"]
europa_gdf
| NAME | geometry | percapita | |
|---|---|---|---|
| 0 | Albania | MULTIPOLYGON (((19.50115 40.96230, 19.50563 40... | 5847 |
| 1 | Andorra | POLYGON ((1.43992 42.60649, 1.45041 42.60596, ... | 42035 |
| 2 | Austria | POLYGON ((16.00000 48.77775, 16.00000 48.78252... | 50277 |
| 3 | Belgium | POLYGON ((5.00000 49.79374, 4.99724 49.79696, ... | 43814 |
| 4 | Bosnia Herzegovina | POLYGON ((19.22947 43.53458, 19.22925 43.53597... | 6727 |
| 5 | Croatia | MULTIPOLYGON (((14.30038 44.50156, 14.28972 44... | 15646 |
| 6 | Czech Republic | POLYGON ((14.82523 50.87399, 14.83687 50.86996... | 24569 |
| 7 | Denmark | MULTIPOLYGON (((11.99978 54.94118, 11.98534 54... | 63829 |
| 8 | Estonia | MULTIPOLYGON (((23.97511 58.09691, 23.96645 58... | 26377 |
| 9 | Finland | MULTIPOLYGON (((22.07310 60.22830, 22.06502 60... | 48461 |
| 10 | France | MULTIPOLYGON (((-2.28137 46.68570, -2.31121 46... | 44995 |
| 11 | Germany | MULTIPOLYGON (((13.11717 54.54924, 13.12529 54... | 51860 |
| 12 | Gibraltar (UK) | POLYGON ((-5.35322 36.15980, -5.34003 36.15975... | 50941 |
| 13 | Greece | MULTIPOLYGON (((24.74283 37.59560, 24.73534 37... | 18168 |
| 14 | Guernsey (UK) | MULTIPOLYGON (((-2.52483 49.48867, -2.52034 49... | 52531 |
| 15 | Hungary | POLYGON ((22.32725 48.36194, 22.32294 48.32583... | 15373 |
| 16 | Ireland | MULTIPOLYGON (((-10.21726 51.74543, -10.22042 ... | 0 |
| 17 | Isle of Man (UK) | MULTIPOLYGON (((-4.38349 54.32594, -4.37470 54... | 84600 |
| 18 | Italy | MULTIPOLYGON (((12.46117 37.88075, 12.45686 37... | 34997 |
| 19 | Jersey (UK) | POLYGON ((-2.03507 49.19905, -2.03699 49.19314... | 45320 |
| 20 | Latvia | POLYGON ((22.14997 57.64129, 22.17712 57.65100... | 19824 |
| 21 | Liechtenstein | POLYGON ((9.53591 47.27354, 9.56037 47.24912, ... | 143151 |
| 22 | Lithuania | MULTIPOLYGON (((26.22030 55.00000, 26.21693 54... | 22752 |
| 23 | Luxembourg | POLYGON ((6.13802 50.13290, 6.14092 50.12927, ... | 113196 |
| 24 | Macedonia | POLYGON ((22.37382 42.32667, 22.37228 42.32576... | 0 |
| 25 | Malta | MULTIPOLYGON (((14.33938 36.00751, 14.33490 36... | 32021 |
| 26 | Monaco | POLYGON ((7.40680 43.73196, 7.40175 43.73449, ... | 185741 |
| 27 | Montenegro | MULTIPOLYGON (((19.22947 43.53458, 19.22795 43... | 7933 |
| 28 | Netherlands | MULTIPOLYGON (((6.05039 53.44675, 6.04538 53.4... | 58003 |
| 29 | Norway | MULTIPOLYGON (((14.82404 68.21677, 14.80662 68... | 67987 |
| 30 | Poland | MULTIPOLYGON (((14.21288 53.86478, 14.21349 53... | 16930 |
| 31 | Portugal | MULTIPOLYGON (((-25.83763 37.88595, -25.83689 ... | 25065 |
| 32 | San Marino | POLYGON ((12.51004 43.99981, 12.51557 43.99566... | 44947 |
| 33 | Serbia | POLYGON ((18.83875 45.90742, 18.86463 45.91018... | 7497 |
| 34 | Slovakia | POLYGON ((18.85061 49.52131, 18.85416 49.51941... | 21529 |
| 35 | Slovenia | POLYGON ((16.56602 46.48372, 16.54178 46.48717... | 27452 |
| 36 | Spain | MULTIPOLYGON (((-5.60904 36.00032, -5.61149 36... | 31178 |
| 37 | Sweden | MULTIPOLYGON (((16.69817 57.43244, 16.69456 57... | 50339 |
| 38 | Switzerland | POLYGON ((9.53591 47.27354, 9.52660 47.25858, ... | 86673 |
| 39 | United Kingdom | MULTIPOLYGON (((-5.33523 51.85830, -5.34557 51... | 46344 |
| 40 | Armenia | MULTIPOLYGON (((45.48185 40.65776, 45.49001 40... | 4527 |
| 41 | Azerbaijan | MULTIPOLYGON (((49.02264 39.19647, 49.02892 39... | 4498 |
| 42 | Belarus | POLYGON ((30.00000 51.48946, 29.95314 51.48722... | 6133 |
| 43 | Bulgaria | POLYGON ((22.68183 44.21765, 22.68898 44.21381... | 11321 |
| 44 | Faeroe Islands (Denmark) | MULTIPOLYGON (((-6.37378 62.23575, -6.38222 62... | 58585 |
| 45 | Georgia | POLYGON ((40.01024 43.38326, 40.01102 43.39860... | 0 |
| 46 | Iceland | MULTIPOLYGON (((-15.38783 64.35684, -15.39691 ... | 57189 |
| 47 | Jan Mayen (Norway) | POLYGON ((-8.13727 71.14619, -8.09449 71.14722... | 0 |
| 48 | Moldova | POLYGON ((27.72524 48.45466, 27.74713 48.45837... | 4638 |
| 49 | Romania | MULTIPOLYGON (((29.59458 44.81385, 29.57739 44... | 14968 |
| 50 | Svalbard (Norway) | MULTIPOLYGON (((20.80218 78.56455, 20.80361 78... | 0 |
| 51 | Turkey | MULTIPOLYGON (((29.11932 40.83066, 29.11734 40... | 0 |
| 52 | Ukraine | MULTIPOLYGON (((33.52988 45.87363, 33.52778 45... | 3881 |
| 53 | Russia | MULTIPOLYGON (((52.65438 71.45161, 52.64216 71... | 11654 |
europa_gdf.plot(column = "percapita", cmap = "gist_rainbow")
<AxesSubplot:>
fig = plt.figure(figsize=(9, 7))
ax = fig.add_subplot(111)
#Map title
title = "Europe Countries" "GDP Per Capita (USD)"
#Title plot with a line between the figur and the text
plt.title(title + "\n", fontsize=19)
#Map plot indicating what figure base (ax) has to take as reference
europa_gdf.plot(column="percapita", cmap = "gist_rainbow", ax = ax)
#Name of the horizontal y vertical axes
ax.set_xlabel("Longitude", fontsize = 13)
ax.set_ylabel("Latitude", fontsize = 13)
#Minumum and maximum values as limits of the color bar
min_p = min(europa_gdf["percapita"])
max_p = max(europa_gdf["percapita"])
#Definition of the graphhical bar
bar = plt.cm.ScalarMappable(cmap="gist_rainbow",
norm=plt.Normalize(vmin = min_p , vmax=max_p))
#It is declared a list where the values of the bar will be saved
bar._A = []
#Location and size of the bar
cax = plt.axes([0.85, 0.15, 0.03, 0.7]) #[xcoord, ycoord, width, long]
# Plot of the graphical bar with the upper variables (bar and cax)
cbar = fig.colorbar(bar, cax=cax)
#Bar title
cbar.set_label("GDP Per Capita (USD)", fontsize = 12)
#Saving of the map in a jpg file with an spicific resolution
plt.savefig("EuropaPIB.jpg", dpi = 200)
#